package com.boxuegu.service;

import com.boxuegu.dao.CardDao;
import com.boxuegu.domain.Card;

import java.io.IOException;


public class CardService {

    CardDao cardDao = new CardDao();

    //判断卡号是否存在
    public boolean isExists(String id) throws IOException {
        //假设id不存在
        boolean exist = false;
        //从库管中调用findAllCard获取对象数组
        Card[] cards = cardDao.findAllCard();
        //遍历每个对象并进行比较
        for (int i = 0; i < cards.length; i++) {
            Card card = cards[i];
            //用户不是null并且用户id和传入的id一样，证明存在

            String[] s=card.getUserId().split(" ");
            if ( s[0].equals(id)) {
                exist = true;
                break;
            }
        }
        return exist;
    }

    //添加用户的方法
    public boolean addUser(Card card) throws IOException {
        return cardDao.addUser(card);
    }

    //查询用户
    public Card[] findAllCard() throws IOException {
        //调用库管对象获取用户数组
        Card[] allCards = cardDao.findAllCard();
        //判断数组是否有信息
        //假设没有信息
        boolean flag = false;
        //遍历获取每一个用户对象
        for (int i = 0; i < allCards.length; i++) {
            Card allCard = allCards[i];
            //用户对象不为null，则flag=true
            if (allCard != null) {
                flag = true;
                break;
            }
        }
        if (flag) {
            return allCards;
        } else {
            return null;
        }

    }

    //删除用户
    public void deleteCardById(String delId) throws IOException {
        //传给库管来操作容器删除
        cardDao.deleteCardById(delId);

    }

    //查找用户余额
    public double cardBalance(String recId) {
        //传给库管来操作容器查询
        return cardDao.cardBalance(recId);
    }
    //将充值后的余额传给库管，来做容器中的数据变化
    public void totalRechargeCard(String recId, double total) {
        cardDao.totalRechargeCard(recId,total);
    }
}